package org.pay.pay.service;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.pay.core.common.constant.PayConstant;
import org.pay.core.common.util.MyLog;
import org.pay.core.entity.PayOrder;
import org.pay.pay.mq.BaseNotify4MchPay;

/**
 * @author: yangshancheng
 * @date: 17/9/9
 * @description:
 */
@Service
public class PayOrderService {

    private static final MyLog _log = MyLog.getLog(PayOrderService.class);

    @Autowired
    private RpcCommonService rpcCommonService;

    @Autowired
    public BaseNotify4MchPay baseNotify4MchPay;

    public PayOrder query(Long mchId, String payOrderId, String mchOrderNo, boolean executeNotify) {
        PayOrder payOrder;
        if(StringUtils.isNotBlank(payOrderId)) {
            payOrder = rpcCommonService.rpcPayOrderService.baseSelectByMchIdAndPayOrderId(mchId, payOrderId);
        }else {
            payOrder = rpcCommonService.rpcPayOrderService.baseSelectByMchIdAndMchOrderNo(mchId, mchOrderNo);
        }
        if(payOrder == null) return null;

        if(executeNotify && (PayConstant.PAY_STATUS_SUCCESS == payOrder.getState() || PayConstant.PAY_STATUS_COMPLETE == payOrder.getState())) {
            baseNotify4MchPay.doNotify(payOrder, false);
            _log.info("业务查单完成,并再次发送业务支付通知.发送完成");
        }
        return payOrder;
    }

    public PayOrder query(Long mchId, String payOrderId, String mchOrderNo) {
        return query(mchId, payOrderId, mchOrderNo, false);
    }

}
